package com.biz.primus.management.promotion.tag;

import com.biz.primus.model.promotion.enums.PromotionTargetLimitTypeEnum;
import java.io.IOException;
import javax.servlet.jsp.JspException;
import javax.servlet.jsp.JspWriter;
import javax.servlet.jsp.tagext.TagSupport;
import lombok.Setter;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;

/**
 * 促销门槛限制方式Jsp Tag
 *
 * @author david-liu
 */
@Setter
@Slf4j
public class PromotionTargetLimitTypeTag extends TagSupport {
    private static final long serialVersionUID = -3906125992625347084L;

    private String selectedType;

    @Override
    public int doStartTag() throws JspException {
        JspWriter jspWriter = pageContext.getOut();
        try {
            for (PromotionTargetLimitTypeEnum limitType : PromotionTargetLimitTypeEnum.values()) {
                if (StringUtils.isNotBlank(selectedType) && StringUtils.equalsIgnoreCase(limitType.name(), selectedType)) {
                    jspWriter.println(String.format("<option value=\"%s\" selected>%s</option>", limitType.name(), limitType.getDesc()));
                } else {
                    jspWriter.println(String.format("<option value=\"%s\">%s</option>", limitType.name(), limitType.getDesc()));
                }
            }
        } catch (IOException e) {
            log.error("促销门槛限制方式Jsp Tag出错", e);
        }
        return super.doStartTag();
    }
}
